package leeon.bbsbrower.actions;

import ipworks.IPWorksException;

import java.util.ArrayList;

import leeon.bbsbrower.models.UserObject;
import leeon.bbsbrower.util.HTMLUtil;
import leeon.bbsbrower.util.HTTPBean;

public class BBSUserAction {
	
	public static final String URL_ONLINE_USER = Config.SERVER_URL + Config.SERVER_CGI + "bbsufind";
	
	public static final String URL_QUERY_USER = Config.SERVER_URL + Config.SERVER_CGI +  "bbsqry";
	
	public static final String URL_ONLINE_FRIEND = Config.SERVER_URL + Config.SERVER_CGI + "bbsfriend";
	
	
	
	/**
	 * 获取所有的用户
	 * @return
	 * @throws TooManyListenersException 
	 * @throws IPWorksException 
	 */
	public static UserObject[] getAllUsers() throws IPWorksException {
		return getUsersByStartWith(null);
	}
	
	/**
	 * 获取在线用户
	 * @return
	 * @throws TooManyListenersException 
	 * @throws IPWorksException 
	 */
	public static UserObject[] getUsersByStartWith(String start) throws IPWorksException {
		HTTPBean http = HTTPBean.newInstance(Config.PROXY_SERVER, Config.PROXY_PORT, Config.PROXY_USER, Config.PROXY_PASSWORD);
		String content = http.get(URL_ONLINE_USER + (start==null?"?search=*":"?search="+start));
		content = HTMLUtil.findStr(content, "<table width=100% border=0 cellpadding=1>", "</table>");
		String[] rows = HTMLUtil.dealTableRows(content);
			
		ArrayList<UserObject> list = new ArrayList<UserObject>();
		if (rows.length > 1) {
			for (int i = 1; i < rows.length; i ++) {
				String[] cells = HTMLUtil.dealTableCells(rows[i]);
				UserObject user = new UserObject(
					cells[0].trim(),
					HTMLUtil.removeHtmlTag(cells[2]).trim(),
					HTMLUtil.removeHtmlTag(cells[3]).trim(),
					cells[4].trim(),
					null,
					(cells[1].trim().length() != 0)
				);					
				list.add(user);
			}
		}

		return list.toArray(new UserObject[0]);
	}
	
	/**
	 * 获取所有在线好友
	 * @return
	 * @throws TooManyListenersException 
	 * @throws IPWorksException 
	 */
	public static UserObject[] getAllFriends() throws IPWorksException {

		HTTPBean http = HTTPBean.newInstance(Config.PROXY_SERVER, Config.PROXY_PORT, Config.PROXY_USER, Config.PROXY_PASSWORD);
		String content = http.get(URL_ONLINE_FRIEND);
		content = HTMLUtil.findStr(content, "<table border=0 width=100% bgcolor=#ffffff>", "</table>");
		String[] rows = HTMLUtil.dealTableRows(content);
			
		ArrayList<UserObject> list = new ArrayList<UserObject>();
		if (rows.length > 1) {
			for (int i = 1; i < rows.length; i ++) {
				String[] cells = HTMLUtil.dealTableCells(rows[i]);
				UserObject user = new UserObject(
					cells[0].trim(),
					HTMLUtil.removeHtmlTag(cells[2]).trim(),
					HTMLUtil.removeHtmlTag(cells[3]).trim(),
					cells[5].trim(),
					cells[4].trim(),
					true
				);
				list.add(user);
			}
		}
			
		return list.toArray(new UserObject[0]);			
	}
	
	/**
	 * 查询某个ID
	 * @return
	 * @throws TooManyListenersException 
	 * @throws IPWorksException 
	 */
	public static String getUserById(String id) throws IPWorksException {
		HTTPBean http = HTTPBean.newInstance(Config.PROXY_SERVER, Config.PROXY_PORT, Config.PROXY_USER, Config.PROXY_PASSWORD);
		String content = http.get(URL_QUERY_USER + "?userid="+id);
		content = HTMLUtil.findStr(content, "<pre class=ansi>", "</pre>");
			
		if (content != null) {
			content = HTMLUtil.removeHtmlTag(content);
			content = HTMLUtil.dealUserLevel(content);
			return content;
		} else {
			return "用户名[" + id + "]不存在"; 
		}
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
	}

}
